<template>
  <div class='container'>
    <span>wuchuanchuan</span>
    <el-form :inline="true" :model=" searchForm" ref="ruleFormRef">
      <el-form-item label="标签名称" prop="tagName">
        <el-input v-model="searchForm.tagName" placeholder="请输入标签名称" size="small"></el-input>
      </el-form-item>
      <el-form-item>
        <!-- <template slot-scope="scope"> -->
        <el-button icon="el-icon-refresh" size="mini" @click="clearClick">清除</el-button>
        <el-button type="primary" icon="el-icon-search" @click="clickLabel" size="mini">搜索</el-button>
        <!-- </template> -->

      </el-form-item>
      <el-form-item>
        <span style="padding-left: 700px">
          <el-button type="success" icon="el-icon-circle-plus" size="mini" @click="addclickLabel">新增标签
          </el-button>
        </span>
      </el-form-item>
    </el-form>
    <el-alert type="warning" show-icon>
      <span>数据共{{total}}条</span>
    </el-alert>
    <!-- 表格 -->
    <el-table :data="tableList" style="width: 100%">
      <el-table-column type="index" label="序号" width="150">
      </el-table-column>
      <el-table-column prop="subjectName" label="所属学科" width="150">
      </el-table-column>
      <el-table-column prop="tagName" label="标签名称" width="200">
      </el-table-column>
      <el-table-column prop="username" label="创建者" width="200">
      </el-table-column>
      <el-table-column prop="addDate" :formatter="dateFormat" label="创建日期" width="200">
      </el-table-column>
      <el-table-column prop="state" label="前台是否显示" width="200">
      </el-table-column>
      <el-table-column prop="" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row.id)" type="text" size="small">修改</el-button>
          <el-popconfirm title="确定要永久删除此条数据吗？" @confirm="delStudy(scope.row.id)">
            <el-button slot="reference" size="small" type="text">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增对话框 -->
    <el-dialog title="新增学科" :visible.sync="addLabelDialogVisible" width="30%">
      <el-form :model="form" :rules="rules" ref="resetFormData">
        <el-form-item prop="subjectName" label="学科名称">
          <el-input v-model="form.subjectName" type="text" placeholder="请输入学科名称" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="isFrontDisplay">
          <el-switch style="display: block" v-model="form.isFrontDisplay" :active-value="1" :inactive-value="0"
            active-color="#13ce66" inactive-text="是否在页面显示">
          </el-switch>
        </el-form-item>
      </el-form>
      <el-button @click="addLabelDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addSureLabelDialogVisible">确 定</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { add, list, remove } from '../../api/hmmm/tags.js'
import moment from 'moment'
export default {
  data () {
    return {
      searchForm: {
        page: 1,
        pageSize: 10,
        tagName: ''
      },
      tableList: [],
      labelForm: {
        page: 1,
        pageSize: 10

      },
      total: 0,
      addLabelDialogVisible: false,
      form: { subjectName: '', isFrontDisplay: '' },
      subjectName: '',
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }

        ]
      }
    }
  },
  methods: {
    // 修改
    handleClick () { },
    // 删除数据
    async delStudy (id) {
      const { data: res } = await remove({ id: id })
      if (res.success) {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.getList()
      } else {
        this.$message({
          message: '删除失败',
          type: 'error'
        })
      }
      console.log(res)
    },
    // 清除搜索框的值
    clearClick () {
      this.$refs.ruleFormRef.resetFields()
    },
    // 搜索框值
    async clickLabel () {
      const { data: res } = await list(this.searchForm)
      this.tableList = res.items
      this.total = res.counts
      console.log(res)
    },
    // 新增
    addclickLabel () {
      this.addLabelDialogVisible = true
    },
    // 新增标签
    async addSureLabelDialogVisible () {
      this.addSureLabelDialogVisible = true
      const { data: res } = await add()
      console.log(res)
    },
    // 获取列表
    async getList () {
      const { data: res } = await list(this.labelForm)
      this.tableList = res.items
      this.total = res.counts
      console.log(res)
    },
    // 格式化日期
    dateFormat (row, column) {
      var date = row[column.property]

      if (date === undefined) {
        return ''
      }

      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped lang='less'></style>
